package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.entity.Result;
import com.itheima.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import static com.itheima.constant.MessageConstant.SEND_VALIDATECODE_FAIL;
import static com.itheima.constant.MessageConstant.SEND_VALIDATECODE_SUCCESS;

@Service(version = "1.0", interfaceClass = ValidateCodeService.class)
@Transactional
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    JedisPool jedisPool;

    @Override
    public Result send4Login(String telephone) {
        /**
         * 发验证码 验证码  telephone
         * 验证码存在redis中, 设置过期时间
         */
        //int code = RandomUtils.nextInt(100000, 999999);
        int code = 123456;
        //boolean flag = sendSms(telephone, code);
        System.out.println("发短信手机号:" + telephone + "   " + "验证码:" + code);
        boolean flag = true;
        if (flag) {
            Jedis jedis = jedisPool.getResource();
            jedis.setex("LOGIN_" + telephone, 300, String.valueOf(code));
            if (jedis != null) {
                jedis.close();
            }
            return Result.success(SEND_VALIDATECODE_SUCCESS, null);
        }
        return Result.fail(SEND_VALIDATECODE_FAIL);
    }
}
